/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-03-17
*/
package com.rzico.basics.service;

import com.rzico.base.BaseMapper;
import com.rzico.base.impl.BaseServiceImpl;
import com.rzico.basics.entity.Brand;
import com.rzico.basics.entity.BrandTag;
import com.rzico.basics.entity.Product;
import com.rzico.basics.entity.ProductTag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.rzico.basics.mapper.BrandMapper;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <pre>
 * 访问统计业务类
 * </pre>
 *
 * @author Rzico Boot
 * @version 1.0
 */
@Service
public class BrandService extends BaseServiceImpl<Brand, String> {

    @Autowired
    private BrandMapper brandMapper;

    @Autowired
    private BrandTagService brandTagService;

    @Override
    public BaseMapper<Brand, String> getMapper() {
        return brandMapper;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteByIds(Object[] ids) {
        int rw = 0;
        for (Object id:ids) {
            Brand brand = selectByPrimaryKey(id);
            brand.setDeleted(true);
            rw = rw + super.updateByPrimaryKeySelective(brand);
        }
        return rw;
    }

    @Transactional(rollbackFor = Exception.class)
    public int save(Brand brand, Long [] tagIds){
        int row = insertUseGeneratedKeys(brand);

        if (tagIds!=null) {
            if (tagIds != null) {
                for (Long tagId : tagIds) {
                    BrandTag brandTag= new BrandTag();
                    brandTag.setBrands(brand.getId());
                    brandTag.setTags(tagId);
                    brandTagService.insert(brandTag);
                }
            }
        }

        return row;

    }


    @Transactional(rollbackFor = Exception.class)
    public int update(Brand brand, Long [] tagIds){
        int row = updateByPrimaryKeySelective(brand);
        BrandTag brandTag = new BrandTag();
        brandTag.setBrands(brand.getId());
        brandTagService.delete(brandTag);

        if (tagIds!=null) {
            if (tagIds != null) {
                for (Long tagId : tagIds) {
                    brandTag= new BrandTag();
                    brandTag.setBrands(brand.getId());
                    brandTag.setTags(tagId);
                    brandTagService.insert(brandTag);
                }
            }
        }

        return row;

    }


}
